<?

echo  "<table label='entryDetail' border='1' class='dedeq'>\n".
      "<th> No     </th>\n".
      "<th> Drop   </th>\n".
      "<th> Cat    </th>\n".
      "<th> Grp    </th>\n".
      "<th> Model  </th>\n".
      //"<th> ID     </th>\n".
      "<th> Unit   </th>\n".
      "<th> Qty    </th>\n".
      "<th> Cvt    </th>\n".
      "<th> Qty    </th>\n".
      "<th> Price  </th>\n".
      "<th> #      </th>\n";

$COracle = new oracleDb;
$COracle->init($_SESSION["ORCL"], 
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

$srcDbCategory  = "MST_MODEL_CAT";
$srcDbGroup     = "MST_MODEL_GRP";
$srcDbModel     = "MST_MODEL";
$srcDbUnit      = "MST_MODEL_UNIT";
   
if($COracle->connect()){      
  
  fetchLstCategory($COracle,
                   $srcDbCategory,
                   $lstCategory);
  
  for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){
    
    /**
    * We check the group and model 
    * If the $_POST is NULL then 
    * it is supposed not to have the list
    * of the dropDown item,
    * then we set an empty array
    * Else we fetch the list manually
    *
    */ 
    if($_POST["groupEnt"][$iRow] == ""){
      
      $lstGroup = array(" ");
    }else{
      
      //$lstGroup = array($_POST["groupEnt"][$iRow]);
      
      fetchLstGroup($COracle,
                    $_POST["categoryEnt"][$iRow],
                    $srcDbGroup,
                    $lstGroup);
    }
    
    if($_POST["modelEnt"][$iRow] == ""){
      
      $lstModel = array(" ");
    }else{
      
      fetchLstModel($COracle,
                    $_POST["groupEnt"][$iRow],
                    $srcDbGroup,
                    $srcDbModel,
                    $lstModel);
    }
  
    $propCategory = " onFocus =' setYellow2(this); 
                               ' ".
                    " onBlur  =' setWhite2(this); 
                               ' ".
                               
                    " onChange='initLstGroup(this.value,
                                             \"groupEnt[".$iRow."]\")' ";
                                             
    $propGroup    = " onFocus =' setYellow2(this); 
                               ' ".
                    " onBlur  =' setWhite2(this); 
                               ' ".
                               
                    " onChange='initLstModel(\"categoryEnt[".$iRow."]\",
                                             this.value,
                                             \"modelEnt[".$iRow."]\")' ";
    //$_SESSION["curLevelD"] = 3;
    $propModel    = " onFocus =' setYellow2(this); 
                               ' ".
                    " onBlur  =' setWhite2(this); 
                               ' ".
                                          
                    " onChange=' ".
                               /** 
                               * Init the list of models
                               *
                               */
                               "getPropModel(this.value,
                                             \"modelIdEnt[".$iRow."]\",
                                             \"unitEnt[".$iRow."]\"); ".
                                             
                               /**
                               * Init the list of convertion 
                               * Based on Convertion level and model's Unit
                               *
                               */
                               "getPropConvertion(".$_SESSION["curLevelD"].",
                                                  \"unitEnt[".$iRow."]\",
                                                  \"cvtEnt[".$iRow."]\"); ".
                                                  
                               "' ";
    
    $propQuantity = " onFocus='setYellow2(this); ".
    
                              "syncVal2(\"qtyEnt[".$iRow."]\",
                                        \"totQtyEnt\")' ".
                                       
                    " onBlur ='sumVal2(\"qtyEnt[".$iRow."]\",
                                       \"totQtyEnt\"); ".
                                      
                              "setWhite2(this)' ";
   
    $propHrzTotal = " onFocus='setYellow2(this); ".
                              
                              "syncVal2(\"hrzTotEnt[".$iRow."]\",
                                        \"grandTotEnt\"); ".
                                        
                              "calcHrzTotal(\"priceEnt[".$iRow."]\",
                                            \"cvtQtyEnt[".$iRow."]\",
                                            \"hrzTotEnt[".$iRow."]\")' ".
                                       
                    " onBlur ='sumVal2(\"hrzTotEnt[".$iRow."]\",
                                       \"grandTotEnt\"); ".
                                      
                              "setWhite2(this)' ";
    
    $propConvertion = " onFocus='setYellow2(this); ".
                      
                      " '".
                      
                      " onBlur ='setWhite2(this); 
                                 calcConvertion(\"qtyEnt[".$iRow."]\",
                                                \"unitEnt[".$iRow."]\",
                                                \"cvtEnt[".$iRow."]\",
                                                \"cvtQtyEnt[".$iRow."]\"); ".
                                " '";
    
    $propCvtQuantity = jsOnFocus("setYellow2(this); 
                                  syncVal2(\"cvtQtyEnt[".$iRow."]\",
                                           \"totCvtQtyEnt\");").
                                           
                       jsOnBlur ("setWhite2(this);
                                  sumVal2(\"cvtQtyEnt[".$iRow."]\",
                                           \"totCvtQtyEnt\");");
    
    $propCheckers    = jsOnChange("syncOrSumVal8(\"dropBox[]\",
                                                 ".$iRow.",
                                                 \"qtyEnt[".$iRow."]\",
                                                 \"totQtyEnt\",
                                                 \"cvtQtyEnt[".$iRow."]\",
                                                 \"totCvtQtyEnt\",
                                                 \"hrzTotEnt[".$iRow."]\",
                                                 \"grandTotEnt\");");
    if($_POST["cvtEnt"][$iRow] == ""){
      
      $lstConvertion = array(" ");
    }else{
      
      fetchLstConvertion($COracle,
                         $_SESSION["curLevelD"],
                         $_POST["unitEnt"][$iRow],
                         $srcDbUnit,
                         $lstConvertion);
    }
    
    echo  "<tr>\n".
            "<td> ".($iRow+1)." </td>\n".
            "<td> ".formCheckBox("dropBox[]",
                                 ($iRow + 1),
                                 $propCheckers)." </td>\n".
            
            "<td> ".formDropDown("categoryEnt[".$iRow."]",
                                 $_POST["categoryEnt"][$iRow],
                                 $lstCategory,
                                 $propCategory)." </td>\n".
            
            "<td> ".formDropDown("groupEnt[".$iRow."]",
                                 $_POST["groupEnt"][$iRow],
                                 $lstGroup,
                                 $propGroup)." </td>\n".
                                                      
            "<td> ".formDropDown("modelEnt[".$iRow."]",
                                 $_POST["modelEnt"][$iRow],
                                 $lstModel,
                                 $propModel)." </td>\n".
                                 
            formHidden("modelIdEnt[".$iRow."]",
                       $_POST["modelIdEnt"][$iRow],
                       _MODE_READONLY).
                              
            "<td> ".formInput("unitEnt[".$iRow."]",
                              $_POST["unitEnt"][$iRow],
                              $stdProperties. _MODE_READONLY)." </td>\n".
                              
            "<td> ".formInput("qtyEnt[".$iRow."]",
                              $_POST["qtyEnt"][$iRow],
                              $numStyle . $propQuantity)." </td>\n".
            
            "<td> ".formDropDown("cvtEnt[".$iRow."]",
                                 $_POST["cvtEnt"][$iRow],
                                 $lstConvertion,
                                 $propConvertion)." </td>\n".
                              
            "<td> ".formInput("cvtQtyEnt[".$iRow."]",
                              $_POST["cvtQtyEnt"][$iRow],
                              $numStyle . $propCvtQuantity)." </td>\n".
                              
            "<td> ".formInput("priceEnt[".$iRow."]",
                              $_POST["priceEnt"][$iRow],
                              $numStyle . $stdProperties)." </td>\n".
            
            "<td> ".formInput("hrzTotEnt[".$iRow."]",
                              $_POST["hrzTotEnt"][$iRow],
                              $numStyle . $propHrzTotal)." </td>\n".
                              
          "</tr>\n";
  }
  
  /**
  *--------------------------------------------------------------------------------------
  * The total goes here
  *
  */
  echo  "<tr>\n".
          "<td colspan='6' align='right'><b> TOTAL </b> </td>\n".
          "<td> ".formInput("totQtyEnt",
                            $_POST["totQtyEnt"],
                            $numStyle)." </td>\n".
                       
          "<td>  </td>\n".
          
          "<td> ".formInput("totCvtQtyEnt",
                            $_POST["totCvtQtyEnt"],
                            $numStyle)." </td>\n".
          
          "<td>  </td>\n".
                            
          "<td> ".formInput("grandTotEnt",
                            $_POST["grandTotEnt"],
                            $numStyle)." </td>\n".
                            
        "</tr>\n";
  
  echo 	"</table><br/><br/>\n".
          formInputButton("Entry-data",
                          "entry",
                          "Entry-data",
                          $stlButton).
    			formInputButton("Add-row",
                          "addrow",
                          "Add-row",
                          $stlButton).
          formInputButton("Drop-row",
                          "droprow",
                          "Drop-row",
                          $stlButton).
    		"</form>	\n";
}else{
  
  usrDie("Oracle connection failed");
}
?>